import socketserver
import json
import configparser
from conf import settings


class ServerHandler(socketserver.BaseRequestHandler):
    def handle(self):
        while True:
            data = self.request.recv(1024).strip()
            data = json.loads(data.decode('utf8'))

        if hasattr(self, data.get('action')):
            func = getattr(self, data.get("action"))
            func(**data)
        else:
            print("invalid cmd")

    def auth(self, **data):  # 接收client 端发过来的数据
        username = data["username"]
        password = data["password"]

        user = self.authenticate(username, password)



    def authenticate(self, user, pwd):  # 服务端验证
        cfg = configparser.ConfigParser()
        cfg.read(settings.ACCOUNT_PATH)

        if user in cig.sections():  # configparser 知识点

            if cfg[user]["Password"] == pwd:
                print("passed authenticatipon")
                return user
            

            
    def put(self, **data):
        pass
